package com.jimiopen.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jimiopen.common.constants.ConstantsString;
import com.jimiopen.common.utils.file.FileManager;

public class RetrivePage {

	private static final Logger log = LoggerFactory
			.getLogger(RetrivePage.class);

	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public static String downLoad(String path) throws IOException {
		URL pageUrl = new URL(path);
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				pageUrl.openStream(), "utf-8"));
		String line;
		StringBuffer pageBuffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			pageBuffer.append(line + "\n");
		}
		return pageBuffer.toString();
	}

	public static void main(String[] args) throws IOException {
		// int value = FileManager.FileOutput(null, "javaio.txt",
		// "hello world");
		int value = FileManager.FileOutput(null, "javaio.txt",
				RetrivePage.downLoad("http://www.baidu.com"));
		if (value > 0) {
			log.info(ConstantsString.SUCCESS);
		} else {
			log.info(ConstantsString.FAILURE);
		}
	}
}
